20 - 图标特效,无限滚动循环练习
图标特效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>49-图标特效</title>
<style>
*{
margin: 0;
padding: 0;
}
ul{
list-style: none;
width: 400px;
height: 250px;
border: 1px solid #000;
margin: 100px auto;
}
ul>li{
width: 100px;
height: 50px;
margin-top: 50px;
text-align: center;
float: left;
overflow: hidden;
}
ul>li>span{
display: inline-block;
width: 24px;
height: 24px;
background: url("images/bg.png") no-repeat 0 0;
position: relative;
}
</style>
<script src="js/jquery-1.12.4.js"></script>
<script>
$(function () {
// 1.遍历所有的li
$("li").each(function (index, ele) {
// 1.1生成新的图片位置
var $url = "url(\"images/bg.png\") no-repeat 0 "+(index * -24)+"px"
// 1.2设置新的图片位置
$(this).children("span").css("background", $url);
});
// 2.监听li移入事件
$("li").mouseenter(function () {
// 2.1将图标往上移动
$(this).children("span").animate({
top: -50
}, 1000, function () {
// 2.2将图片往下移动
$(this).css("top", "50px");
// 2.3将图片复位
$(this).animate({
top: 0
}, 1000);
});
});
});
</script>
</head>
<body>
<ul>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
<li><span></span><p>百度</p></li>
</ul>
</body>
</html>
无限循环滚动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>50-无限循环滚动</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width: 600px;
height: 161px;
border: 1px solid #000;
margin: 100px auto;
overflow: hidden;
}
ul{
list-style: none;
width: 1800px;
height: 161px;
background: #000;
}
ul>li{
float: left;
}
</style>
<script src="js/jquery-1.12.4.js"></script>
<script>
$(function () {
// 0.定义变量保存偏移位
var offset = 0;
// 1.让图片滚动起来
var timer;
function autoPlay(){
timer = setInterval(function () {
offset += -10;
if(offset <= -1200){
offset = 0;
}
$("ul").css("marginLeft", offset);
}, 50);
}
autoPlay();
// 2.监听li的移入和移出事件
$("li").hover(function () {
// 停止滚动
clearInterval(timer);
// 给非当前选中添加蒙版
$(this).siblings().fadeTo(100, 0.5);
// 去除当前选中的蒙版
$(this).fadeTo(100, 1);
}, function () {
// 继续滚动
autoPlay();
// 去除所有的蒙版
$("li").fadeTo(100, 1);
});
});
</script>
</head>
<body>
<div>
<ul>
<li><img src="images/a.jpg" alt=""></li>
<li><img src="images/b.jpg" alt=""></li>
<li><img src="images/c.jpg" alt=""></li>
<li><img src="images/d.jpg" alt=""></li>
<li><img src="images/a.jpg" alt=""></li>
<li><img src="images/b.jpg" alt=""></li>
</ul>
</div>
</body>
</html>
原文作者: 冯亚杰(DanBoard·Feng)
原文链接: http://danbo3110.github.io/2019/10/23/20 - 图标特效,无限滚动循环练习/
版权声明: 转载请注明出处(必须保留作者署名及链接)